package de.baumann.browser.activitys.duobao

import android.app.Activity
import android.content.Intent
import de.baumann.browser.R
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.duobao.IDuoBaoPhoneView
import de.baumann.browser.present.duobao.DuoBaoPhonePresenter
import de.baumann.browser.units.TimerUnit
import kotlinx.android.synthetic.main.activity_duo_bao_phone.*
import kotlinx.android.synthetic.main.activity_register.*
import kotlinx.android.synthetic.main.include_tool_bar_2.*

class DuoBaoPhoneActivity : BaseOdinActivity<IDuoBaoPhoneView, DuoBaoPhonePresenter>(), IDuoBaoPhoneView {

    companion object {
        fun startDuoBaoPhoneActivity(context: Activity) {
            context.startActivityForResult(Intent(context, DuoBaoPhoneActivity::class.java), 2010)
        }
    }

    private var timerUnit: TimerUnit? = null

    override fun getBackIcon(): Int {
        return R.drawable.ic_back
    }


    override fun getTitleText(): String {
        return "手机号码"
    }

    override fun getLayout(): Int {
        return R.layout.activity_duo_bao_phone
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        duobaoGetSmsCode.setOnClickListener {
            timerUnit = TimerUnit(duobaoGetSmsCode)
            timerUnit!!.startTime()
            presenter.duobaoSendSms()
        }

        btnLJBD.setOnClickListener {
            presenter?.duobaoyz()
        }
    }

    override fun onError(errorMsg: String?) {
        super.onError(errorMsg)
        endTimer()
    }

    override fun onError(errorMsgId: Int) {
        super.onError(errorMsgId)
        endTimer()
    }

    private fun endTimer() {
        if (timerUnit != null) {
            timerUnit!!.endtTime()
        }
    }

    override fun initData() {

    }

    override fun createPresenter(): DuoBaoPhonePresenter {
        return DuoBaoPhonePresenter()
    }

    override fun createView(): IDuoBaoPhoneView {
        return this
    }

    override fun bdcg() {
        setResult(Activity.RESULT_OK, Intent().apply {
            putExtra("phone", telNum)
        })
        finish()
    }

    override val telNum: String
        get() = duobaoPhone.text.toString().trim()
    override val smsCode: String
        get() = duobaoSmsCode.text.toString().trim()
}